package com.kim.androidbase.db;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

import com.kim.androidbase.db.IDBUtils;
import com.kim.androidbase.db.IDBUtils.QueryCompleteCallback;

public class DBSession {

	private final static byte[] dbLock = new byte[0]; // 同步锁

	SQLiteOpenHelper dbHelper;
	SQLiteDatabase db;

	public DBSession(Context context, SQLiteOpenHelper dbHelper) {
		this.dbHelper = dbHelper; // new DBHelper(context,
									// databaseErrorHandler);
	}

	private void open() {
		synchronized (dbLock) {
			try {
				db = dbHelper.getWritableDatabase();
			} catch (SQLiteException ex) {
				db = dbHelper.getReadableDatabase();
			}
		}
	}

	private void close() {
		if (db != null) {
			db.close();
		}
	}

	/**
	 * 插入单条记录
	 * 
	 * @param tableName
	 *            表名称
	 * @param contentValues
	 *            记录
	 * @param database
	 *            数据库
	 * @param conflictAlgorithm
	 *            冲突解决方法
	 */
	public final void insert(String tableName, ContentValues contentValues,
			int conflictAlgorithm) {
		synchronized (dbLock) {
			open();
			IDBUtils.insert(tableName, contentValues, db, conflictAlgorithm);
			close();
		}
	}

	/**
	 * 批量插入记录
	 * 
	 * @param tableName
	 *            表名称
	 * @param contentValues
	 *            记录列表
	 * @param database
	 *            数据库
	 * @param conflictAlgorithm
	 *            冲突解决方法
	 */
	public final void insertBatch(String tableName,
			List<ContentValues> contentValuesList, int conflictAlgorithm) {
		synchronized (dbLock) {
			open();
			IDBUtils.insertBatch(tableName, contentValuesList, db,
					conflictAlgorithm);
			close();
		}
	}

	/**
	 * 删除记录
	 * 
	 * @param tableName
	 *            表名称
	 * @param database
	 *            数据库
	 * @param whereClause
	 *            条件
	 * @param whereArgs
	 *            条件参数
	 */
	public void delete(String tableName, String whereClause, String[] whereArgs) {
		synchronized (dbLock) {
			open();
			IDBUtils.delete(tableName, db, whereClause, whereArgs);
			close();
		}
	}

	/**
	 * 修改记录
	 * 
	 * @param tableName
	 *            表名称
	 * @param database
	 *            数据库
	 * @param whereClause
	 *            条件
	 * @param whereArgs
	 *            条件参数
	 */
	public void modify(String tableName, ContentValues contentValues,
			String whereClause, String[] whereArgs) {
		synchronized (dbLock) {
			open();
			IDBUtils.modify(tableName, contentValues, db, whereClause,
					whereArgs);
			close();
		}
	}

	/**
	 * 查询记录
	 * 
	 * @param tableName
	 *            表名称
	 * @param database
	 *            数据库
	 * @param columns
	 *            要查询的列
	 * @param selection
	 *            查询条件
	 * @param selectionArgs
	 *            条件参数
	 * @param groupBy
	 *            分组
	 * @param having
	 *            having子句
	 * @param orderBy
	 *            排序
	 * @param queryCompleteCallback
	 *            查询回调接口，处理游标
	 */
	public void query(String tableName, String[] columns, String selection,
			String[] selectionArgs, String groupBy, String having,
			String orderBy, QueryCompleteCallback queryCompleteCallback) {
		synchronized (dbLock) {
			open();
			IDBUtils.query(tableName, db, columns, selection, selectionArgs,
					groupBy, having, orderBy, queryCompleteCallback);
			close();
		}
	}

	/**
	 * 
	 * @param sql
	 *            查询语句
	 * @param selectionArgs
	 *            条件参数
	 * @param database
	 *            数据库
	 * @param queryCompleteCallback
	 *            查询回调接口，处理游标
	 */
	public void query(String sql, String[] selectionArgs,
			QueryCompleteCallback queryCompleteCallback) {
		synchronized (dbLock) {
			open();
			IDBUtils.query(sql, selectionArgs, db, queryCompleteCallback);
			close();
		}
	}

	public Cursor rawQuery(String sql, String[] selectionArgs) {
		synchronized (dbLock) {
			try {
				open();
				return IDBUtils.rawQuery(sql, selectionArgs, db);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
			}
			return null;
		}
	}

	public String getString(Cursor cursor, String columnName) {
		return cursor.getString(cursor.getColumnIndex(columnName));
	}

	public int getInt(Cursor cursor, String columnName) {
		return cursor.getInt(cursor.getColumnIndex(columnName));
	}

	public long getLong(Cursor cursor, String columnName) {
		return cursor.getLong(cursor.getColumnIndex(columnName));
	}

	public float getFloat(Cursor cursor, String columnName) {
		return cursor.getFloat(cursor.getColumnIndex(columnName));
	}

	public boolean getBoolean(Cursor cursor, String columnName) {
		return cursor.getInt(cursor.getColumnIndex(columnName)) > 0;
	}

	public String getString(Cursor cursor, int columnIndex) {
		return cursor.getString(columnIndex);
	}

	public int getInt(Cursor cursor, int columnIndex) {
		return cursor.getInt(columnIndex);
	}

	public long getLong(Cursor cursor, int columnIndex) {
		return cursor.getLong(columnIndex);
	}

	public float getFloat(Cursor cursor, int columnIndex) {
		return cursor.getFloat(columnIndex);
	}

	public boolean getBoolean(Cursor cursor, int columnIndex) {
		return cursor.getInt(columnIndex) > 0;
	}

}
